package com.controller;

import java.util.Collection;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.subject.support.DefaultSubjectContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.service.StudentService;

/****
 * 用户登录Controller
 *
 * @author Swinglife
 *
 */
@Controller
public class LoginController {
	// 处理用户业务类
	@Autowired
	private StudentService studentService;
	@Autowired
	EnterpriseCacheSessionDAO sessionDao;

	@GetMapping(value = "/")
	public String home(Model model){
		System.out.println("kkkk");
		return "home";
	}
	/***
	 * 跳转到登录页面
	 *
	 * @return
	 */
	@GetMapping(value = "/login")
	public String show(Model model){
		System.out.println("kkkk");
		return "accountlogin";
	}


	@PostMapping(value = "/login")
	public String Login(String username, String password, String rememberMe, Model model) {
		System.out.println(username + "--------------------1111 " + password + " " + rememberMe);
		Subject subject = SecurityUtils.getSubject();

		UsernamePasswordToken token = new UsernamePasswordToken(username, password);

		//禁止已登录用户
//		Collection<Session> sessions = sessionDao.getActiveSessions();
//        for (Session session : sessions) {
//
//            System.out.println("登录用户" + session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY));
//            if (session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY) != null) {
//                return  new ModelAndView("accountlogin", "msg", "该用户已登录");
//            }
//        }



		boolean remembermeRusult = rememberMe == null ? false : true; // null=>false
		if (remembermeRusult) {
			token.setRememberMe(remembermeRusult);
		}
		String error = null;
		try {
			subject.login(token);
		} catch (UnknownAccountException e) {
			error = "用户名错误";
		} catch (IncorrectCredentialsException e) {
			error = "用户名/密码错误";
		} catch (ExcessiveAttemptsException e) {
			// TODO: handle exception
			error = "登录失败多次，账户锁定10分钟";
		} catch (AuthenticationException e) {
			// 其他错误，比如锁定，如果想单独处理请单独catch处理
			e.printStackTrace();
			error = "其他错误：" + e.getMessage();
		}
		if (error != null) {// 出错了，返回登录页面
			return "accountlogin";
		} else {// 登录成功
			return "home";
		}

	}

//	@RequestMapping("login")
//	public String login(){
//		return "login";
//	}

}